<?php
/**********************************************************
 		薪资单据审核流程
 **********************************************************/
namespace src\logic\workflow;
use \classlib\input;
use \classlib\core as C;
use \src\logic\workflow\doc_basic;
class doc_salary extends doc_basic {
	//系统申请
	public function node1_submit(&$state) {
		return '0';
	}
	//运营审核
	public function node2_submit(&$state) {
		$orm  = C\model::load('pro.pl.salary');
		$data = $orm->get_item(array('executeid'=>$state['executionid']));
		//提交请求的话更新薪资修正之后的数值
		if (strcasecmp('post', $this->method()) == 0) {
			$disname = \src\logic\Cadmin::operator();
			$remark  = input::get_post('remark');
			if ($data) {
				$field   = array('remark'=>$remark, 'ip'=>C\get_ip(),
					'audittime'=>C\config::$nowtime, 'operator'=>$disname);
				//审核通过的话 记录记录登记
				if ($state['status'] == 6) {
					$salary = (float)input::get_post('salary');
					if ($salary >= 0) {
						$field['salary'] = $salary;
					}
					$bonus  = (float)input::get_post('bonus');
					if ($bonus >= 0) {
						$field['bonus'] = $bonus;
					}
					$field['state']  = 1;
					$field['status'] = 0;
					$orm->save($data['id'], $field);
				} else {
					$field['state']  = 2;
					$orm->save($data['id'], $field);
					$userid= \src\logic\Cadmin::CREATEOR_USERID;
					$data  = array('stopuserid'=>$userid, 'stopusername'=>$disname, 'remark'=>'[System]',
						'status'=>\src\logic\workflow\doc_executor::TERMINATE, 'stoptime'=>C\config::$nowtime);
					C\model::load('main.proc.execution')->save($state['executionid'], $data);
				}
			}
		} else {
			//支付默认的应发奖金金额
			if (!isset($state['json']) || empty($state['json'])) {
				$state['json'] = json_encode(array('salary'=>$data['salary'], 'bonus'=>$data['bonus']));
			}
		}
		return '0';
	}
	
	//运营主管审核
	public function node3_submit(&$state) {
		$orm  = C\model::load('pro.pl.salary');
		$data = $orm->get_item(array('executeid'=>$state['executionid']));
		//提交请求的话更新薪资修正之后的数值
		if (strcasecmp('post', $this->method()) == 0) {
			$disname = \src\logic\Cadmin::operator();
			$remark  = input::get_post('remark');
			if ($data) {
				$field   = array('remark'=>$remark, 'ip'=>C\get_ip(),
						'audittime'=>C\config::$nowtime, 'operator'=>$disname);
				//审核通过的话 记录记录登记
				if ($state['status'] == 6) {
					$field['status'] = 1;
					$orm->save($data['id'], $field);
					self::dotransfer($data);
				} else {
					$field['state']  = 0;
					$field['status'] = 2;
					$orm->save($data['id'], $field);
				}
			}
		}
		return '-1';
	}
	
	//查询数据库更新用户余额信息
	public static function dotransfer($data) {
		$db  = C\model::load('pro.pl.anchor')->get_orm()->get_parser();
		$db->begin_transaction();
		try {
			//更新奖金DB信息
			$table = C\model::load('pro.pl.anchor')->get_table();
			$coins = intval($data['salary'] * \src\logic\lconfig::STARRATIO);
			$sql   = sprintf('update %s set salary=salary+%f, salarycoins=salarycoins+%d,'.
				' earningcoins=earningcoins+%d, lastcoins=lastcoins+%d where userid=%d',
				$table, $data['salary'], $coins, $coins, $coins, $data['userid']);
			$db->execute($sql);
			$table = C\model::load('pro.log.moneyflow')->get_table();
			$sql   = sprintf('insert into %s (userid, anchorid, sid, eventid, reftype, refid, coinprice,'.
				' earning, nums, ip, stime) values(0, %d, 0, 0, 5, 0, %d, %d, 1, "%s", %d);', 
				$table, $data['userid'], $coins, $coins, C\get_ip(), C\config::$nowtime);
			$db->execute($sql);
		} catch (\Exception $e) {
			$db->rollback();
			\classlib\logger::inst()->write($e->getMessage());
			return false;
		}
		$db->commit();
		C\model::load('pro.pl.anchor')->set_ver();
		C\model::load('pro.log.moneyflow')->set_ver();
		return true;
	}
}
